﻿CREATE PROCEDURE [ServiceEngine].[ListRuntimeEventDataForBindingID]
	@BindingID uniqueidentifier
AS

BEGIN
	SET NOCOUNT ON;

--bindings for resources
	SELECT DISTINCT rse.RuntimeServerID, rse.EndpointID, ov.OperationID
	FROM ServiceEngine.OperationVersions ov
		INNER JOIN ServiceEngine.Endpoints_AssociatedOperations eao ON eao.OperationVersionID = ov.OperationVersionID
		INNER JOIN ServiceEngine.RuntimeServers_Endpoints rse ON eao.EndpointID = rse.EndpointID
		INNER JOIN ServiceEngine.Resources r ON r.ResourceID = ov.ResourceID
	WHERE r.BindingID = @BindingID
	UNION
	SELECT DISTINCT rse.RuntimeServerID, rse.EndpointID, ov.OperationID
	FROM ServiceEngine.OperationVersions ov
		INNER JOIN ServiceEngine.Endpoints_PublishedOperations epo ON epo.OperationVersionID = ov.OperationVersionID
		INNER JOIN ServiceEngine.RuntimeServers_Endpoints rse ON epo.EndpointID = rse.EndpointID
		INNER JOIN ServiceEngine.Resources r ON r.ResourceID = ov.ResourceID
	WHERE r.BindingID = @BindingID
	UNION
--bindings for endpoints
	SELECT DISTINCT rse.RuntimeServerID, rse.EndpointID, null
	FROM ServiceEngine.RuntimeServers_Endpoints rse 
		INNER JOIN ServiceEngine.EndPoints e ON e.EndPointID = rse.EndpointID
	WHERE e.BindingID = @BindingID
	UNION
--bindings for runtime servers
	SELECT DISTINCT rs.RuntimeServerID, null, null
	FROM ServiceEngine.RuntimeServers rs
	WHERE rs.CatalogBindingID = @BindingID
		OR rs.NotificationBindingID = @BindingID
		OR rs.ListenerBindingID = @BindingID
		OR rs.BrokerBindingID = @BindingID
END